<template>
  <div class="main">
    <div class="card" :class="{'gridCard': false}" @click="$router.push({path: '/goodsDetail/' + item.goods_id})" v-for="item in data" :key="item.goods_id">
      <div class="left">
        <img class="goodsImage" :src="item.goods_img" alt="" v-imgField />
      </div>
      <div class="right">
        <p class="goodsName"><i class="self" v-if="item.is_self">自营</i>{{ item.goods_name }}</p>
        <p class="goodsTag">
          <span>好评率 {{item.goods_positive || 0}}% 已售出 {{ item.goods_salenum.toFixed(0) || 0}}</span>
        </p>
        <p class="goodsPrice"
           v-if="!item.goods_marketprice==0">
          <em>￥</em>{{ +item.goods_marketprice }}
          <em>会员价：{{ +item.goods_saleprice }}</em>
        </p>
        <p class="goodsPrice"
           v-if="item.goods_marketprice==0">
          <em>￥</em>{{ item.goods_saleprice.toFixed(2) }}
        </p>
        <button class="goodsShop" @click.stop="joinShoppingCart()"><img src="../../../../assets/img/icon_shop.png" alt=""></button>
      </div>
    </div>
  </div>
</template>

<script>
import myPanel from '../panel'
import { getGoodsList, addShoppingCart } from '../../../../api/index.js'
import goodsCard from '../../../../components/card/goodsCard'
import qs from 'qs'
import { SET_LOADING } from '@store/type'
import { mapMutations } from 'vuex'

export default {
  props: {
    category: {},
    parentCategoryId: ''
  },
  components: {
    myPanel,
    goodsCard
  },
  data () {
    return {
      data: []
    }
  },
  created () {
    this.fetchGetGoodsList()
  },
  updated () {
    // console.log(this.parentCategoryId)
  },
  methods: {
    ...mapMutations({
      setLoading: SET_LOADING
    }),
    // 请求商品列表接口
    async fetchGetGoodsList () {
      let form = {
        a: '',
        category_id: this.parentCategoryId || 0, // 分类id
        keyword: '',
        page: 1,
        rows: 10,
        sord: 'desc',
        sidx: 'seller_distance'
      }
      // form.page = page.num
      // form.rows = page.size
      form.longitude = 0
      form.latitude = 0

      let res = await getGoodsList(qs.stringify(form))
      if (res && res.code === 200) {
        this.data = res.data.list
      }
    },
    // 加入购物车
    async joinShoppingCart () {
      // if (!this.isCanBuy()) {
      //   this.$refs.specPanel.show()
      //   return
      // }

      this.setLoading({
        status: true,
        opacity: 0
      })
      let goods = this.data
      let valueids = null
      let valuenames = null
      let skuid = null
      if (goods.is_enablespec === 1 && goods.goods_sku) {
        skuid = goods.goods_sku.sku_id
        valueids = goods.goods_sku.sku_valueids
        valuenames = goods.goods_sku.sku_valuenames
      }
      let shoppingCarGoods = {
        seller_id: goods.seller_id,
        goods_id: goods.goods_id,
        goods_name: goods.goods_name,
        sku_id: skuid,
        sku_valueids: valueids,
        sku_valuenames: valuenames,
        goods_num: goods.goods_buynum,
        goods_saleprice: goods.goods_saleprice,
        goods_saletype: goods.goods_saletype,
        shipping_method: 0,
        is_appointment: goods.is_enableappoint
      }
      let res = await addShoppingCart(qs.stringify(shoppingCarGoods))
      this.setLoading(false)
      if (res && res.code === 200) {
        this.$toastText(res.msg)
        // this.$refs.specPanel.hide()
      }
    },
    handleClick (item) {
      this.$router.push({
        path: '/goodsList',
        query: {
          a: 'category',
          cid: item.category_id,
          name: item.category_name
        }
      })
    }
  },
  watch: {
    parentCategoryId () {
      this.fetchGetGoodsList()
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../../assets/css/pub";

.main {
  flex: 1;
  width: 75%;
  overflow-y: scroll;
  background-color: #fff;
  .swiper-img {
    width: 92%;
    height: 92%;
    margin-top: .3125rem;
    margin-left: .3125rem;
  }
}

.card {
  width: 100%;
  display: inline-block;
  font-size: 0;
  .card-item {
    width: 33.333333%;
    height: 2.75rem;
    display: inline-block;
    text-align: center;
    a {
      height: 100%;
      display: flex;
      flex-direction: column;
      padding-top: .125rem;
      box-sizing: border-box;
    }
    img {
      width: 1.5rem;
      height: 1.5rem;
      margin: 0 auto;
    }
    .text {
      margin-top: .525rem;
      font-size: .3125rem;
      color: #666666;
      @include textHideByLine(1);
    }
  }
}

.hasNoData {
  font-size: .325rem;
  background-color: #fff;
  text-align: center;
}

.swiper-ad {
  height: 3.7rem;
}

.card {
  padding: .25rem;
  display: flex;
  background-color: #fff;

  .left {
    width: 2.5rem;
    height: 2.5rem;
    padding: .2rem;
    flex: none;

    .goodsImage {
      width: 100%;
      height: 100%;
      padding: 0;
      // border-radius: 500px;
    }
  }
  .right {
    padding-left: .25rem;
    // width: 100%;
    width: calc(100% - 3rem);
    height: 2.5rem;
    position: relative;
    // 商品名称
    .goodsName {
      height: 1rem;
      line-height: .53rem;
      font-size: $default_font_size;
      color: $default_title_color;
      vertical-align: top;
      @include textHideByLine(2);
      i.self {
        font-style: normal;
        margin-right: .125rem;
        padding: 0 .125rem;
        background-color: $default_theme_color;
        color: #fff;
        font-size: $default_smallfont_size;
      }
    }
    // 商品价格
    .goodsPrice {
      margin-top: .25rem;
      /*height: .55rem;*/
      font-size: $default_bigfont_size;
      color: $default_price_color;
      overflow: visible;
      em {
        font-size: $default_smallfont_size;
        font-style: normal;
      }
      .reduce {
        border: .025rem solid $default_red_color;
        color: $default_red_color;
        padding: 0 .125rem;
        border-radius: .2rem;
        font-size: $default_middlefont_size;
      }
    }
    // 商品标签
    .goodsTag {
      margin-top: .2rem;
      height: .4rem;
      font-size: $default_middlefont_size;
      color: $default_subtitle_color;
      overflow: visible;
    }
    // 商品店铺
    .goodsSeller {
      margin-top: .2rem;
      height: .4rem;
      font-size: $default_middlefont_size;
      display: flex;
      align-items: center;
      .sellerName {
        flex: 0 1 calc(100% - 1.65rem);
        line-height: .4rem;
        @include textHideByLine(1);
      }
      .sellerLogo {
        width: .4rem;
        height: .4rem;
        border-radius: .125rem;
        margin-right: .125rem;
      }
      .sellerDistance {
        width: 1.5rem;
        text-align: right;
      }
    }
    // 购物按钮
    .goodsShop{
      position: absolute;
      right: 0;
      bottom: .6rem;
      width: .8rem;
      height: .8rem;
      background: #fff;
      border: 1px solid #fd6a6a;
      border-radius: 500px;
      color: #fd6a6a;
      padding: .15rem;
      img{
        width: 100%;
        height: 100%;
      }
    }
  }
}

.gridCard {
  display: inline-block;
  width: 4.87rem;
  height: 7.75rem;
  padding: .25rem;
  box-sizing: border-box;
  flex-direction: column;
  .left {
    width: 4.0625rem;
    height: 4.0625rem;
  }
  .right {
    width: 4.0625rem;
    padding-left: 0;
    padding-top: .25rem;
  }
  &:nth-child(2n) {
    margin-left: .25rem;
  }
}
</style>
